import { useUserStore } from '@/stores'
import { getToken } from '@/utils/utils'
import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
//全局进度条的配置 
NProgress.configure({
	easing: 'ease', // 动画方式 
	speed: 300, // 递增进度条的速度 
	showSpinner: false, // 进度环显示隐藏
	trickleSpeed: 200, // 自动递增间隔 
	minimum: 0.3, // 更改启动时使用的最小百分比 
	parent: 'body', //指定进度条的父容器 
});
const routes: Array<RouteRecordRaw> = [
	{
		path: '/',
		name: 'layout',
		redirect: '/index',// 重定向 下面二级路由的那个路由
		component: () => import('@/layout/index.vue'),
		children: [
			{
				//首页
				path: '/index',
				name: 'Index',
				meta: {
					title: '首页',
					isLogin: false,//是否需要登录
					// KeepName: 'hcwl-course-index',//vue3 需要配置组件名
					// KeepAlive: true, // 组件需要缓存
				},
				component: () => import('@/views/index/index.vue')
			},
			{
				//课程分类
				path: '/course',
				name: 'Course',
				meta: {
					title: '课程分类',
					isLogin: true,
					isShow: true,//是否在头部展示
					// KeepName: 'hcwl-course-index',//vue3 需要配置组件名
					// KeepAlive: true, // 组件需要缓存
				},
				component: () => import('@/views/course/index.vue')
			},
			{
				//学习中心
				path: '/study',
				name: 'Study',
				meta: {
					title: '学习中心',
					isLogin: true,
					isShow: true,//是否在头部展示
					// KeepName: 'hcwl-course-index',//vue3 需要配置组件名
					// KeepAlive: true, // 组件需要缓存
				},
				component: () => import('@/views/study/index.vue')
			},
			{
				//收藏课程
				path: '/collect',
				name: 'Collect',
				meta: {
					title: '收藏课程',
					isLogin: true,
					// KeepName: 'hcwl-course-index',//vue3 需要配置组件名
					// KeepAlive: true, // 组件需要缓存
				},
				component: () => import('@/views/collect/index.vue')
			},
			{
				//搜索
				path: '/search',
				name: 'Search',
				meta: {
					title: '搜索',
					isLogin: true,
					// KeepName: 'hcwl-course-index',//vue3 需要配置组件名
					// KeepAlive: true, // 组件需要缓存
				},
				component: () => import('@/views/search/index.vue')
			},
			{
				//视频课程
				path: '/videoc',
				name: 'Videoc',
				meta: {
					title: '视频课程',
					isLogin: true,
					// KeepName: 'hcwl-course-index',//vue3 需要配置组件名
					// KeepAlive: true, // 组件需要缓存
				},
				component: () => import('@/views/videoc/index.vue')
			},
		]
	},

	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/login/index.vue')
	},
	{
		path: '/:catchAll(.*)',
		name: '404',
		component: () => import('@/views/NotFound/index.vue')
	}
]
const router = createRouter({
	history: createWebHashHistory(import.meta.env.BASE_URL),
	scrollBehavior(to, from, savedPosition) {
		// 始终滚动到顶部
		return { top: 0 }
	},
	routes,
})
/* 路由守卫设置 */
router.beforeEach((to, from, next) => {
	NProgress.start();
	const store = useUserStore()
	const token = getToken()
	//需要登录的页面
	if (to.meta.isLogin) {
		//判断是否登录
		if (token) {
			let toName = ''
			//跳转的页面是视频页面 ，从首页 ，视频页面跳过来的排除
			if (to.name === 'Videoc' && from.name !== 'Index' && from.name !== 'Videoc') {
				//记录一下上一页的标题 
				toName = from.meta.title as string
				//记录上一页标题 路径 参数 视频页面跳转上一页需要
				const { meta, path, query } = from
				store.path.title = meta.title
				store.path.path = path
				store.path.query = query
			}
			//跳转的页面是首页 ，从视频页面跳过来的排除
			if (from.name === 'Index') {
				store.path.title = ''
				store.path.from = ''
			}
			next()
		} else {
			store.LoginVisible = true
			// 清除登录信息
			// store.clearLogin()
			next('/index')
		}
	}
	else {
		//不需要登录的页面
		next()
	}

})

router.afterEach(() => {
	// 结束进度条  
	NProgress.done();
});
export default router